﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using hospital_registration_system.DAL;
using hospital_registration_system.Entity;

namespace hospital_registration_system.BLL
{
    /// <summary>
    /// 科室表服务
    /// </summary>
    public class DepartmentsService
    {
        protected DepartmentsDao dao = null;
        public DepartmentsService()
        {
            dao = new DepartmentsDao();
        }
        /// <summary>
        /// 查找所有的科室
        /// </summary>
        /// <returns>返回所有科室的链表</returns>
        public List<Departments> selectAllDepartments()
        {
            return dao.selectAll();
        }

        /// <summary>
        /// 删除指定id的科室
        /// </summary>
        /// <param name="id">要删除科室的id</param>
        /// <returns>操作是否成功</returns>
        public bool deleteDepartmentsById(int id)
        {
            if (id < 0) return false;//检查id属性是否符合要求
            return dao.delete(id);
        }

        /// <summary>
        /// 修改科室信息
        /// </summary>
        /// <param name="departments">要修改的科室信息</param>
        /// <returns></returns>
        public bool updateDepartments(Departments departments)
        {
            if (departments == null || departments.Id < 0)
            {
                return false;
            }
            return dao.update(departments);
        }

        /// <summary>
        /// 插入新的科室信息
        /// </summary>
        /// <param name="name">要插入的科室名</param>
        /// <returns></returns>
        public bool insertDepartments(String name)
        {
            Departments departments = new Departments();
            departments.Department_name = name;
            return dao.insert(departments);
        }

        /// <summary>
        /// 通过疾病id在疾病科室表中查询符合条件的科室
        /// </summary>
        /// <returns></returns>
        public List<Departments> findDepartmentByIllnessId(int illnessId)
        {
            Illness_departmentsDao dao2 = new Illness_departmentsDao();
            List<Illness_departments> illDep = dao2.selectByIllnessId(illnessId);
            List<Departments> ret = new List<Departments>();
            foreach (Illness_departments d in illDep)
            {
                ret.Add(dao.selectById(d.Department_id));
            }
            return ret;
        }
    }
}
